#include "rocket_2.h"

extern int G_H,G_W;

ROCKET_2::ROCKET_2(QPoint begin, bool left_orientation, SCENE *scene, QTimeLine *global_tl, CASH *cash):QGraphicsObject()
{
    this->cash = cash;

    shot = new SOUND_POWER(cash,"Res/Sound/Enemy/Enemy_2/shot.ogg",this);
    shot->Play_Now();

    point=35;

    this->left_orientation = left_orientation;

    if(left_orientation){
        im = cash->cash_pixmap->value("Res/weapons/rocket_2/rocket_2_left.png");
    }
    else{
        im = cash->cash_pixmap->value("Res/weapons/rocket_2/rocket_2_right.png");
    }

    border = new QPainterPath();

    this->setZValue(0.5);
    this->setPos(begin);

    anim_rocket = new QGraphicsItemAnimation();

    rocket_time_line = new QTimeLine(1000);
    rocket_time_line->setEasingCurve(QEasingCurve::Type(7));
    rocket_time_line->setFrameRange(0,1);

    anim_rocket->setTimeLine(rocket_time_line);
    anim_rocket->setItem(this);
    anim_rocket->setPosAt(0,begin);

    if(left_orientation)
        anim_rocket->setPosAt(1,QPointF(G_W+50,begin.y()+300) ); // Поиграться с 300 надо если ракета галимо летит
    else
        anim_rocket->setPosAt(1,QPointF(-50,begin.y()+300) );

    rocket_time_line->start();

    collisia_control = new COLLISIA_CONTROL(scene,((QGraphicsObject*)this),this,global_tl,cash,10);

    connect(collisia_control,SIGNAL(Collisia(QPointF,int)),this,SLOT(Damage_Detected(QPointF,int)));
    connect(rocket_time_line,SIGNAL(finished()),SLOT(deleteLater()));

    QTimer::singleShot(20, this, SLOT(set_shape()));
}

QPainterPath ROCKET_2::shape() const
{

    return *border;

}

QRectF ROCKET_2::boundingRect() const
{

    return QRectF( QPointF(0,0),im->size() );

}

void ROCKET_2::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

    painter->save();
    painter->drawPixmap( 0,0,*im );
    painter->restore();

}

ROCKET_2::~ROCKET_2()
{
}

int ROCKET_2::type() const
{

    return point;

}

void ROCKET_2::Damage_Detected(QPointF a, int d)
{

    a = QPointF();
    point-=d;

    if(point<=0){
        shot->Stop();
        this->scene()->removeItem(this);
    }

}

void ROCKET_2::set_shape()
{
    if(left_orientation){
        border = cash->cash_poly->value("Res/weapons/rocket_2/rocket_2_left.poly");
    }
    else{
        border = cash->cash_poly->value("Res/weapons/rocket_2/rocket_2_right.poly");
    }
}

